#include<bits/stdc++.h>
using namespace std;
int N = 9999999;
struct node {
	int dist, id;
	node(int dist, int id) {
		this->dist = dist;
		this->id = id;
	}
};
struct edge {
	int from, end, w;
};
bool operator <(node a, node b) { return a.dist > b.dist; }
bool visited[100010];
int dist[1000010];
vector<edge>v[100010];
int main(void)
{
	int n, m;
	cin >> n >> m;
	memset(dist, N, sizeof(dist));
	priority_queue<node>q;
	for (int i = 0; i < m; i++)
	{
		int s, e, w;
		cin >> s >> e >> w;
		edge ee;
		ee.from = s;
		ee.end = e;
		ee.w = w;
		v[s].push_back(ee);
		v[e].push_back(ee);
	}
	int current = 1;
	dist[1] = 0;
	node t = { current,1 };
	q.push(t);
	while (q.size())
	{
		node temp = q.top();
		q.pop();
		if (visited[temp.id])
			continue;
		visited[temp.id] = true;
		for (int i = 0; i < v[temp.id].size(); i++)
		{
			int newdist = dist[temp.id] + v[temp.id][i].w;
			if (newdist < dist[v[temp.id][i].end])
			{
				dist[v[temp.id][i].end] = newdist;
				node newnode = { newdist,v[temp.id][i].end };
				q.push(newnode);
			}
		}
	}
	cout << dist[n];


}